package com.atguigu.orderservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.orderservice.entity.TOrder;
import com.atguigu.orderservice.service.TPayLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-04
 */
@Api(description = "支付管理")
@RestController
@RequestMapping("/orderservice/paylog")
@CrossOrigin
public class TPayLogController {

    @Autowired
    private TPayLogService payLogService;

    @ApiOperation(value = "根据订单编号获得微信支付二维码")
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        //很多数据封装为map了
        Map<String,Object> map =  payLogService.createNative(orderNo);
        return R.ok().data(map);
    }
    @ApiOperation(value = "根据订单编号查询微信支付状态")
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        //1调用service查询支付状态,因为有成功和失败所以是map
        Map<String,String> map =  payLogService.queryPayStatus(orderNo);
        //2 判断是否为空
        if (map == null) {
            return R.error().message("支付出错");
        }
        if ("SUCCESS".equals(map.get("trade_state"))) {
            //3支付成功，更新订单状态
            payLogService.updateOrderInfo(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }


}

